package cn.tristenstudy.blog.service.impl;

import cn.tristenstudy.blog.exception.MyNotFoundException;
import cn.tristenstudy.blog.mapper.BlogMapper;
import cn.tristenstudy.blog.mapper.TypeMapper;
import cn.tristenstudy.blog.pojo.Blog;
import cn.tristenstudy.blog.pojo.Type;
import cn.tristenstudy.blog.pojo.dto.TypeDTO;
import cn.tristenstudy.blog.service.TypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xichen1.li
 * @date 2020-12-25 16:53
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private BlogMapper blogMapper;

    @Transactional
    @Override
    public Boolean saveType(Type type) {
        type.setCreateTime(new Date());
        type.setUpdateTime(new Date());
        int insert = typeMapper.insert(type);
        return insert > 0;
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        return typeMapper.selectById(id);
    }

    @Transactional
    @Override
    public Page<Type> listType(Long current) {
        Page<Type> page = new Page<>();
        page.setCurrent(current);
        page.setSize(3);
        return typeMapper.selectByCondition(page);
    }

    @Override
    public List<Type> list() {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        queryWrapper.select("id", "name");
        queryWrapper.orderByDesc("create_time");
        return typeMapper.selectList(queryWrapper);
    }

    @Override
    public List<TypeDTO> listTypeTop(Integer size) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        List<TypeDTO> result = new ArrayList<>();
        queryWrapper.eq("del_flag", 0);
        List<Type> typeList = typeMapper.selectList(queryWrapper);
        for (Type type : typeList) {
            TypeDTO typeDTO = new TypeDTO();
            typeDTO.setId(type.getId());
            typeDTO.setName(type.getName());
            QueryWrapper<Blog> blogQueryWrapper = new QueryWrapper<>();
            blogQueryWrapper.eq("type_id", type.getId());
            blogQueryWrapper.eq("del_flag", 0);
            blogQueryWrapper.eq("published",true);
            Integer count = blogMapper.selectCount(blogQueryWrapper);
            typeDTO.setBlogCount(count);
            result.add(typeDTO);
        }
        return result.stream()
                .sorted((type1, type2) -> type2.getBlogCount() - type1.getBlogCount())
                .limit(size)
                .collect(Collectors.toList());
    }

    @Transactional
    @Override
    public Boolean updateType(Type type) {
        Type select = typeMapper.selectById(type.getId());
        if (select == null) {
            throw new MyNotFoundException("分类不存在！");
        }
        type.setUpdateTime(new Date());
        int rows = typeMapper.updateById(type);
        return rows > 0;
    }

    @Transactional
    @Override
    public Boolean deleteType(Long id) {
        Type type = typeMapper.selectById(id);
        if (type == null) {
            throw new MyNotFoundException("分类不存在！");
        }
        type.setDelFlag(1);
        type.setUpdateTime(new Date());
        int row = typeMapper.updateById(type);
        return row > 0;
    }

    @Override
    public boolean isNotExist(Type type) {
        return typeMapper.selectByName(type.getName()) == null;
    }
}
